"""
    在读写二进制文件时显示下载进度
"""
import requests
from tqdm import tqdm  # 第三方包 显示下载进度条


def download_vidio(url, save_path):
    response = requests.get(url, stream=True)

    total_size = int(response.headers.get('content-length', 0))

    # 初始化大小
    download_size = 0

    with open(save_path, 'wb') as f, tqdm(total=total_size, unit='b', unit_scale=True, unit_divisor=1024) as bar:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
                download_size += len(chunk)
                bar.update(len(chunk))
    print("下载完成")


vidio_url = ('https://www.douyin.com/aweme/v1/play/?video_id=v0300fg10000csea5rnog65id7beqrpg&line=0&file_id'
             '=7c53c9cd3a65450fafcf58e5be406abb&sign=ff0a8d81fae85069b4b985bf208d70cc&is_play_url=1&source'
             '=PackSourceEnum_FEED')
path = './douyin.mp4'
download_vidio(vidio_url, path)
